package com.sdcqjy.mylibrary.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sdcqjy.mylibrary.Logger;
import com.sdcqjy.mylibrary.R;

/**
 * 也可以使用Snackbar
 */
public class ShowToast {
    public static void alertShort(Context context, String msg) {
        if (context != null && msg != null) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    }

    public static void alertLong(Context context, String msg) {
        if (context != null && msg != null) {
            Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        }
    }

    private ShowToast() {
    }

    private static ShowToast showToast;

    private synchronized static void init() {
        if (showToast == null)
            showToast = new ShowToast();
    }

    public static ShowToast getInstance() {
        if (showToast == null) init();
        return showToast;
    }

    public static void alertShortOfWhite(Context context, String msg) {
        init();
        showToast.alertOfWhite(context, msg, 0);
    }

    public static void alertShortOfWhite(Context context, int msgRes) {
        init();
        String msg = context.getResources().getString(msgRes);
        showToast.alertOfWhite(context, msg, 1);
    }

    public static void alertLongOfWhite(Context context, String msg) {
        init();
        showToast.alertOfWhite(context, msg, 1);
    }

    private ToastModel toastModel;
    private Toast toast;
    private TextView textView;

    public ShowToast setToastModel(ToastModel toastModel) {
        this.toastModel = toastModel;
        return this;
    }

    private void alertOfWhite(Context context, String msg, int duration) {
        Logger.v("Toast", msg);
        //alertShort(context, msg);
        if (toast == null) {
            if (toastModel != null) {
                toast = toastModel.getToast(context);
                textView = toastModel.getTextView();
            } else {
                toast = new Toast(context);
                View view = LayoutInflater.from(context).inflate(R.layout.toast_show, null);
                textView = (TextView) view.findViewById(R.id.textView1);
                toast.setView(view);
                //int v = (int) /*(ScreenUtils.getStatusHeight(context) + */context.getResources().getDimension(R.dimen.title_height);
                //int screenWidth = ScreenUtils.getScreenWidth(context);
                //textView.getLayoutParams().width = screenWidth;
                //toast.setGravity(Gravity.TOP, 0, v);
                //toast.setGravity(Gravity.BOTTOM, 0, 0);
            }
        }
        if (context != null && msg != null) {
            textView.setText(msg);
            toast.setDuration(duration);
            toast.show();
        }
    }

    public interface ToastModel {
        Toast getToast(Context context);

        TextView getTextView();
    }
}
